﻿using System;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls.Maps;
using Spotted.Data.Args;
using Spotted.Data.Entities;

namespace Spotted.Controls
{
    public partial class SpottedMapView : UserControl
    {
        public static readonly DependencyProperty ZoomLevelProperty =
            DependencyProperty.Register("ZoomLevel", typeof (double), typeof (SpottedMapView),
                                        new PropertyMetadata(ZoomLevelChanged));

        public static readonly DependencyProperty CenterLocationProperty =
            DependencyProperty.Register("CenterLocation", typeof (GeoCoordinate), typeof (SpottedMapView),
                                        new PropertyMetadata(CenterLocationChanged));

        public static readonly DependencyProperty LocationsProperty =
            DependencyProperty.Register("Places", typeof (ObservableCollection<Place>), typeof (SpottedMapView),
                                        new PropertyMetadata(PlacesChanged));

        public event EventHandler<PlaceSelectedEventArgs> PlaceSelected; 

        public SpottedMapView()
        {
            InitializeComponent();
        }

        #region Properties

        public double ZoomLevel
        {
            get { return theMap.ZoomLevel; }
            private set { theMap.ZoomLevel = value; }
        }

        public GeoCoordinate CenterLocation
        {
            get { return theMap.Center; }
            private set
            {
                theMap.Center = value;
                var pp = new Pushpin { Location = value, Style = Application.Current.Resources["centerPushpin"] as Style };
                theMap.Children.Add(pp);
            }
        }

        private ObservableCollection<Place> _places; 
        public ObservableCollection<Place> Places
        {
            get { return _places; }
            private set
            {
                _places = value;

                foreach (var place in value)
                {
                    var location = new GeoCoordinate(place.Latitude, place.Longitude);
                    var pp = new Pushpin
                                 {
                                     Location = location,
                                     Style = Application.Current.Resources["locationPushpin"] as Style,
                                     DataContext = place
                                 };

                    pp.Tap += pp_Tap;
                    theMap.Children.Add(pp);
                }
            }
        }

        void pp_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var place = (Place) ((Pushpin) sender).DataContext;
            if (PlaceSelected != null)
            {
                PlaceSelected(this, new PlaceSelectedEventArgs(place));
            }
        }

        #endregion

        #region Property Changed Event Handlers

        private static void ZoomLevelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var mapView = (SpottedMapView) d;
            mapView.ZoomLevel = (double)e.NewValue;
        }

        private static void CenterLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var spottedMap = (SpottedMapView) d;
            spottedMap.CenterLocation = (GeoCoordinate) e.NewValue;
        }

        private static void PlacesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var spottedMap = (SpottedMapView) d;
            spottedMap.Places = (ObservableCollection<Place>) e.NewValue;
        }

        #endregion
    }
}